//highmap坐标转换工具
import proj4 from 'proj4';
// 以下坐标转换函数来自 https://www.highcharts.com.cn/docs/latlon
// Highmaps 默认的坐标系统参数
let transform = {
    "crs": "+proj=mill +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R_A +datum=WGS84 +units=m +no_defs",
    "scale":1.70830296554e-05,
    "jsonres":15.5,
    "jsonmarginX":-999,
    "jsonmarginY":9851.0,
    "xoffset":-19816494.5204,
    "yoffset":12635908.1982
};
/**
 * 经纬度转换函数
 * @param float lon 经度
 * @param float lat 纬度
 * @return [x, y]
 */
function lonlatToXY(lon, lat) {
    if(typeof lon === 'string') {
        lon = parseFloat(lon);
    }

    if(typeof lat === 'string') {
        lat = parseFloat(lat);
    }

    let projected = proj4(transform.crs, [lon, lat]),
        cosAngle = transform.cosAngle || (transform.rotation && Math.cos(transform.rotation)),
        sinAngle = transform.sinAngle || (transform.rotation && Math.sin(transform.rotation)),
        rotated = transform.rotation ? [projected[0] * cosAngle + projected[1] * sinAngle, -projected[0] * sinAngle + projected[1] * cosAngle] : projected;
    return [
        parseInt(((rotated[0] - (transform.xoffset || 0)) * (transform.scale || 1) + (transform.xpan || 0)) * (transform.jsonres || 1) + (transform.jsonmarginX || 0)),
        -parseInt((((transform.yoffset || 0) - rotated[1]) * (transform.scale || 1) + (transform.ypan || 0)) * (transform.jsonres || 1) - (transform.jsonmarginY || 0))
    ];
}

//options for echarts in GlobalOverview
const WorldMapOption = {
    darkMode: true,
    title: {
        text: '世界现存确诊病例地图',
        textStyle: {
            fontFamily: 'pangmen',
            fontSize: 50,
            color: 'silver',
            fontWeight: 'normal',
        },
        subtext: '数据更新日期',
        subtextStyle: {
            fontFamily: ['iceland','pangmen'],
            fontSize: 30,
            color: '#ffffff',
            fontWeight: 100,
        },
        right: '20',
        left: 'auto',
        z: 100,
    },
    geo: {
        id: 'world',
        name: '世界国家地图',
        type: 'map',
        map: 'WORLD',
        zoom: 1,
        roam: true,
        scaleLimit: {min: 1},
        center: lonlatToXY(30,20),
        itemStyle: {
            areaColor: 'gray',
        },
        label: {
            color: '#8B0000',
            fontFamily: 'iceland',
            fontSize: 30,
            textBorderColor: 'gray',
            textBorderWidth: 3,
            textBorderType: 'solid',
        },
        emphasis: {
            itemStyle: {
                areaColor: 'silver',
                shadowColor: 'silver',
                shadowBlur: 10,
            },
            label: {
                color: '#8B0000',
                fontFamily: 'iceland',
                fontSize: 30,
                textBorderColor: 'gray',
                textBorderWidth: 3,
                textBorderType: 'solid',
            }
        },
        select: {
            itemStyle: {
                areaColor: 'silver',
                shadowColor: 'silver',
                shadowBlur: 20,
            },
            label: {
                color: '#8B0000',
                fontFamily: 'iceland',
                fontSize: 30,
                textBorderColor: 'gray',
                textBorderWidth: 3,
                textBorderType: 'solid',
            },
        },
        z: 1,
    },
    visualMap: [{
        type: 'continuous',
        seriesIndex: 1,
        geoIndex: 'world',
        min: 0,
        max: 10000,
        text: ['High', 'Low'],
        realtime: false,
        calculable: true,
        show: true,
        inRange: {
            color: ['#168023', '#D09E00', '#7F3A0B', '#540000']
        },
        textStyle: {
            color: 'silver',
            fontFamily: 'iceland',
            fontSize: 20,
        },
        left: 'auto',
        right: 20,
    }]
}
const WorldMapLoading = {
    maskColor: 'rgba(0,0,0,0.8)',
    text: '  正在聚合...',
    color: 'gray',
    spinnerRadius: 15,
    textColor: 'gray',
    fontFamily: 'pangmen',
    fontSize: 50,
}
const WorldMapPinSeries = {
    type: 'scatter',
    coordinateSystem: 'geo',
    symbol: 'circle',
    symbolSize: (val) => val[2],
    itemStyle: {
      color: 'red',
    },
    largeThreshold: 500,    //500+个点启动大规模数据优化
    encode: {
        value: 2,           //data第2个维度的值设为value
    },
    data: [
        {name:'海门', value:lonlatToXY(121.15,31.89).concat(0)},
        {name:'鄂尔多斯', value:lonlatToXY(109.781327,39.608266).concat(0)},
        {name:'招远', value:lonlatToXY(120.38,37.35).concat(0)},
        {name:'舟山', value:lonlatToXY(122.207216,29.985295).concat(0)},
        {name:'齐齐哈尔', value:lonlatToXY(123.97,47.33).concat(0)},
        {name:'盐城', value:lonlatToXY(120.13,33.38).concat(0)},
        {name:'赤峰', value:lonlatToXY(118.87,42.28).concat(0)},
    ],
    silent: true,
    z: 3,
}
const WorldMapAreaSeries = {
    type: 'map',
    geoIndex: 0,
    nameProperty: 'name',
    data: [],
    silent: true,
    z: 2,
}

export default {
    lonlatToXY,
    WorldMapOption,
    WorldMapLoading,
    WorldMapPinSeries,
    WorldMapAreaSeries,
}